package giromba.display.loading
{	
	import giromba.display.ResizeableSpriteBase;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class LoadingWheel extends ResizeableSpriteBase
	{
		[Embed(source="../embeds/assets.swf", symbol="giromba.assets.ALoadingWheel")]
		private var ALoadingWheel:Class;
		
		protected var asset:DisplayObject;
		protected var container:Sprite;
		
		public function LoadingWheel(asset:DisplayObject = null)
		{
			super();
			this.asset = asset || new ALoadingWheel();
			build();
			width = this.asset.width;
			height = this.asset.height;
		}
		
		private function build():void{
			container = new Sprite();
			addChild(container);
			container.addChild(asset);
			asset.x = - asset.width/2;
			asset.y = - asset.height/2;
			container.visible = false;
		}
		
		override public function show():void{
			container.visible = true;
			addEventListener(Event.ENTER_FRAME, rotate);
		}
		
		override public function hide():void{
			hideComplete();
		}
		
		private function hideComplete():void{
			container.visible = false;
			removeEventListener(Event.ENTER_FRAME, rotate);
		}
		
		protected function rotate(event:Event):void{
			container.rotation+=10;
		}
		
		override protected function render():void{
			asset.width = width;
			asset.height = height;
			asset.x = - width/2;
			asset.height = -height/2;
			container.x = width/2;
			container.y = height/2;
		}
	}
}